package com.gzs.zhdj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gzs.zhdj.constant.MessageConstant;
import com.gzs.zhdj.context.BaseContext;
import com.gzs.zhdj.domain.dto.ActPageDto;
import com.gzs.zhdj.domain.dto.ActivitiesDto;
import com.gzs.zhdj.domain.dto.PageBean;
import com.gzs.zhdj.domain.dto.ParticipantDto;
import com.gzs.zhdj.domain.entity.Activities;
import com.gzs.zhdj.domain.entity.PartyMember;
import com.gzs.zhdj.exception.FileErrorException;
import com.gzs.zhdj.exception.JoinException;
import com.gzs.zhdj.mapper.ActivityMapper;
import com.gzs.zhdj.service.ActivityService;
import com.gzs.zhdj.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Override
    public void createAct(ActivitiesDto act) {
        activityMapper.createAct(act);
    }

    @Override
    public void joinAct(int aId) {
        int pId = BaseContext.getCurrentId();
        ParticipantDto participant = activityMapper.getTwoId(pId,aId);

        if(participant != null){
            throw new JoinException(MessageConstant.REGISTERED);
        }
        activityMapper.joinAct(pId,aId);
        activityMapper.countAdd(aId);

    }

    @Override
    public void cancelAct(int aId) {
        int pId = BaseContext.getCurrentId();
        activityMapper.cancelAct(pId,aId);
        activityMapper.countCut(aId);
        ParticipantDto participant = activityMapper.getTwoId(pId,aId);
        if(participant != null){
            throw new JoinException(MessageConstant.CANCELFAIL);
        }
    }

    @Override
    public PageBean getAct(String name, LocalDateTime start, LocalDateTime end, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        Page<Activities> actpage = activityMapper.pageQuery(name,start,end);

        long total = actpage.getTotal();
        List<Activities> records = actpage.getResult();
        return new PageBean(total, records);
    }

    @Override
    public void uploadFeel( int aId,MultipartFile file) {
        try {
            int pId = BaseContext.getCurrentId();
            //原始文件名
            String originalFilename = file.getOriginalFilename();

            String filePath = aliOssUtil.upload(file.getBytes(), originalFilename);
            activityMapper.uploadFeel(pId,aId, filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<ParticipantDto> getFeel(int id) {
        List<ParticipantDto> participants = activityMapper.getFeel(id);
        return participants;
    }

    @Override
    public List<ParticipantDto> getSelfFeeel(int id) {
        ParticipantDto participantDto = new ParticipantDto();
        participantDto.setPId(BaseContext.getCurrentId());
        participantDto.setAId(id);
        List<ParticipantDto> participants = activityMapper.getSelfFeel(participantDto);
        return participants;
    }

    @Override
    public void deleteAct(int id) {
        activityMapper.deleteAct(id);
    }

    @Override
    public ActivitiesDto getActById(int id) {
        return activityMapper.getActById(id);
    }

    @Override
    public void updateAct(ActivitiesDto act) {
        activityMapper.updateAct(act);
    }

    @Override
    public PageBean getSelfAct(String name, LocalDateTime start, LocalDateTime end, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        int pId = BaseContext.getCurrentId();
        Page<Activities> pages = activityMapper.pageQueryById(pId,name,start,end);
        long total = pages.getTotal();
        List<Activities> records = pages.getResult();

        return new PageBean(total, records);
    }
}
